﻿Imports System.Collections.Specialized

Public Class MoneyIn
    Private _ParamsValues As New Hashtable
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        _ParamsValues.Clear()

        Dim date3 As Date = New Date(dt月别开始时间.Value.Year, dt月别开始时间.Value.Month, 1)
        Dim date4 As Date = New Date(dt月别结束时间.Value.Year, dt月别结束时间.Value.Month, 1)
        If date3 > date4 Then
            ShowMessageBox("开始时间不能大于结束时间")
            dt月别开始时间.Focus()
            Exit Sub
        End If

        Dim date1 As New Date(Me.dt月别开始时间.Value.Year, Me.dt月别开始时间.Value.Month, 1)
        Dim date2 As New Date(Me.dt月别结束时间.Value.Year, Me.dt月别结束时间.Value.Month, Date.DaysInMonth(Me.dt月别结束时间.Value.Year, Me.dt月别结束时间.Value.Month))
        _ParamsValues.Add("开始时间", date1.ToString)
        _ParamsValues.Add("结束时间", date2.ToString)

        If Me.txtUsername.Text.Trim <> "" AndAlso Me.CheckBox1.Checked Then
            _ParamsValues.Add("用户", CStr(Me.txtUsername.Tag))
        End If
        Try
            Dim englishFolder As String = System.IO.Path.Combine(GetCurrentDirectory, "Report")
            If Not System.IO.Directory.Exists(englishFolder) Then
                System.IO.Directory.CreateDirectory(englishFolder)
            End If

            Dim englishPath As String = System.IO.Path.Combine(englishFolder, "In_Detail.xls")
            Dim bCreate As Boolean = True
            If System.IO.File.Exists(englishPath) Then
                System.IO.File.Delete(englishPath)
            End If

            If bCreate Then
                Dim dt As System.Data.DataTable = CreateDataSourceInMonth()
                If dt.Rows.Count = 0 Then
                    ShowMessageBox("没有数据")
                    Exit Sub
                End If

                Dim realindex As Integer = 1
                Dim newTable As DataTable = dt.Clone
                Dim dv As DataView = dt.DefaultView

                If chk按用户.Checked = False Then
                    Dim englishCreate As New InDetail(englishPath)
                    englishCreate.报表记录条数字 = Me.报表记录条数字
                    englishCreate.报表字体 = Me.报表字体

                    AddHandler englishCreate.PrintProcess, AddressOf PrintProcess

                    For jndex As Integer = 0 To dt.Rows.Count - 2
                        For kdex As Integer = jndex + 1 To dt.Rows.Count - 1
                            If dt.Rows(jndex)("年月") = dt.Rows(kdex)("年月") Then
                                dt.Rows(kdex)("年月") = ""
                            End If
                        Next
                    Next

                    Dim 合计金额 As Decimal = dt.Compute("sum(金额)", "")
                    Dim dr As DataRow = dt.NewRow
                    dr("内容") = "合计金额"
                    dr("金额") = 合计金额
                    dt.Rows.Add(dr)

                    englishCreate.DoOutData(dt)
                    RemoveHandler englishCreate.PrintProcess, AddressOf PrintProcess
                Else
                    Dim englishCreate As New In_User(englishPath)
                    englishCreate.报表记录条数字 = Me.报表记录条数字
                    englishCreate.报表字体 = Me.报表字体

                    AddHandler englishCreate.PrintProcess, AddressOf PrintProcess

                    Dim dtUser As DataTable = ct.FillTable("select * from WysInputUser where type=0 order by userindex")

                    If (_ParamsValues.Contains("用户")) Then
                        dtUser = ct.FillTable("select * from WysInputUser where type=0 and id ='" & _ParamsValues.Item("用户").ToString & "'")
                    End If

                    If dtUser.Rows.Count > 0 Then
                        For index As Integer = 0 To dtUser.Rows.Count - 1
                            dv.RowFilter = "用户='" + CStr(dtUser.Rows(index)("username")) + "'"
                            newTable = dv.ToTable
                            If newTable.Rows.Count > 0 Then

                                For jndex As Integer = 0 To newTable.Rows.Count - 2
                                    For kdex As Integer = jndex + 1 To newTable.Rows.Count - 1
                                        If newTable.Rows(jndex)("年月") = newTable.Rows(kdex)("年月") Then
                                            newTable.Rows(kdex)("年月") = ""
                                        End If
                                    Next
                                Next

                                Dim 合计金额 As Decimal = newTable.Compute("sum(金额)", "")
                                Dim dr As DataRow = newTable.NewRow
                                dr("内容") = "合计金额"
                                dr("金额") = 合计金额
                                newTable.Rows.Add(dr)

                                If index = dtUser.Rows.Count - 1 Then
                                    englishCreate.DoOutData(newTable, realindex, True)
                                Else
                                    englishCreate.DoOutData(newTable, realindex, False)
                                End If

                                realindex = realindex + 1
                            End If
                        Next
                        englishCreate.特别保存()
                    End If

                    RemoveHandler englishCreate.PrintProcess, AddressOf PrintProcess
                End If

                '打开文件
                Using pro As New Process
                    pro.StartInfo.FileName = englishPath
                    pro.Start()
                End Using

            End If
        Catch ex As Exception
            ShowMessageBox(ex.ToString)
        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

#Region "窗体数据保存的重载方法 A-2010-09-06"
    Public Overrides Sub 保存窗体数据到数据库(ByRef dtData As System.Data.DataTable)

        dtData.Rows.Add(New Object() {"开始时间.年", "", dt月别开始时间.Value.Year.ToString})
        dtData.Rows.Add(New Object() {"开始时间.月", "", dt月别开始时间.Value.Month.ToString})

        dtData.Rows.Add(New Object() {"结束时间.年", "", dt月别结束时间.Value.Year.ToString})
        dtData.Rows.Add(New Object() {"结束时间.月", "", dt月别结束时间.Value.Month.ToString})

        If chk按用户.Checked Then
            dtData.Rows.Add(New Object() {"分类1", "", "按用户"})
        End If

    End Sub

    Public Overrides Sub 存取数据库数据到窗体(ByVal dtDbData As System.Data.DataTable)
        If dtDbData.Rows.Count > 0 Then

            If 查找数据("分类1", "", "", dtDbData) = "按用户" Then
                chk按用户.Checked = True
            End If

        End If

        Dim date1 As New Date(CInt(查找数据("开始时间.年", "", Me.dt月别开始时间.Value.Year.ToString, dtDbData)), CInt(查找数据("开始时间.月", "", "1", dtDbData)), 1)
        Me.dt月别开始时间.Value = date1
        date1 = New Date(CInt(查找数据("结束时间.年", "", Me.dt月别开始时间.Value.Year.ToString, dtDbData)), CInt(查找数据("结束时间.月", "", "1", dtDbData)), 1)
        Me.dt月别结束时间.Value = date1
    End Sub

#End Region

    Private Sub btnUsername_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUsername.Click
        Using frm As wys.Control.Vb.ListForm = New wys.Control.Vb.ListForm
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = "select ID,UserName as 名称 from WysInputUser where type=0 order by userindex"
            Dim 显示列 As New StringCollection
            显示列.Add("名称")
            frm.Width = 100
            frm.Height = 200
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                txtUsername.Tag = CStr(frm.返回值(0))
                txtUsername.Text = CStr(frm.返回值(1))
            End If
        End Using
    End Sub

    Private Sub MoneyOut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.txtUsername.Tag = ""
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If Me.CheckBox1.Checked Then
            Me.txtUsername.Enabled = True
            Me.btnUsername.Enabled = True
        Else
            txtUsername.Text = ""
            Me.txtUsername.Enabled = False
            Me.btnUsername.Enabled = False
        End If
    End Sub

#Region "创建收入月别数据源"
    Private Function CreateDataSourceInMonth() As DataTable
        Dim sb As New System.Text.StringBuilder
        sb.AppendLine(" SELECT WysInput.*  ")
        sb.AppendLine(" ,XscBugType.Name ")
        sb.AppendLine(" ,WysInputUser.username username1")
        sb.AppendLine(" FROM WysInput ")
        sb.AppendLine(" LEFT JOIN XscBugType ")
        sb.AppendLine(" ON WysInput.type=XscBugType.id ")
        sb.AppendLine(" LEFT JOIN WysInputUser ")
        sb.AppendLine(" ON WysInput.username=WysInputUser.id ")
        sb.AppendLine(" WHERE date([DateTime])>='" + DateTime.Parse(_ParamsValues.Item("开始时间")).ToString("yyyy-MM-dd") + "'")
        sb.AppendLine(" AND date([DateTime])<='" + DateTime.Parse(_ParamsValues.Item("结束时间")).ToString("yyyy-MM-dd") + "'")
        If (_ParamsValues.Contains("用户")) Then
            sb.AppendLine(" AND WysInput.UserName ='" & _ParamsValues.Item("用户").ToString & "'")
        End If
        sb.AppendLine(" ORDER BY WysInput.DateTime ")

        Dim dt As DataTable = ct.FillTable(sb.ToString)
        If dt.Rows.Count = 0 Then
            Return New DataTable
        End If

        Dim dc As DataColumn
        Dim dt月别 As New DataTable
        dc = dt月别.Columns.Add("内容")
        dc.DefaultValue = ""

        'dt月别.Columns.Add("日期", GetType(Date))
        dc = dt月别.Columns.Add("类型编号")
        dc.DefaultValue = ""

        dc = dt月别.Columns.Add("类型内容")
        dc.DefaultValue = ""

        dc = dt月别.Columns.Add("金额", GetType(Decimal))
        dc.DefaultValue = "0"

        'dt月别.Columns.Add("年月日")
        dc = dt月别.Columns.Add("年月")
        dc.DefaultValue = ""

        'dt月别.Columns.Add("年")
        dc = dt月别.Columns.Add("备注")
        dc.DefaultValue = ""

        dc = dt月别.Columns.Add("用户")
        dc.DefaultValue = ""

        Dim dr月别 As DataRow
        For Each dr As DataRow In dt.Rows
            dr月别 = dt月别.NewRow
            dr月别("内容") = dr("Title")
            'dr月别("日期") = dr("DateTime")
            dr月别("类型编号") = dr("TYPE")
            dr月别("类型内容") = dr("NAME")
            dr月别("金额") = dr("COUNT")
            'dr月别("年月日") = CDate(dr("DateTime")).ToString("yyyyMMdd")
            dr月别("年月") = CDate(dr("DateTime")).ToString("yyyy年MM月")
            'dr月别("年") = CDate(dr("DateTime")).ToString("yyyy")
            dr月别("备注") = dr("memo")
            dr月别("用户") = dr("username1")
            dt月别.Rows.Add(dr月别)
        Next

        Return dt月别

    End Function

    Private Sub PrintProcess(ByVal content As String)
        lbl1000.Text = content
        System.Windows.Forms.Application.DoEvents()
    End Sub
#End Region

#Region "报表必须使用"
    Private _报表字体 As String = "宋体"
    Public Overrides Property 报表字体() As String
        Get
            Return _报表字体
        End Get
        Set(ByVal value As String)
            _报表字体 = value
        End Set
    End Property

    Private _报表记录条数字 As Integer = 55
    Public Overrides Property 报表记录条数字() As Integer
        Get
            Return _报表记录条数字
        End Get
        Set(ByVal value As Integer)
            _报表记录条数字 = value
        End Set
    End Property
#End Region

End Class
